<?php defined('SYSPATH') or die('No direct script access.');

// Статьи
class Model_Pages
{
    // Получаем чистую страницу, без всяких джойнов
    public function get_page($page_id)
    {
        $query = DB::select()
                    ->from('pages')
                    ->where('page_id', '=', $page_id);
                
        $pages = $query->execute()->as_array();

        return $pages[0]; 
    }
    
    public function get_page_by_url($url)
    {
        $query = DB::select()
                    ->from('pages')
                    ->where('url', '=', $url);
                
        $pages = $query->execute()->as_array();

        return $pages[0];   
    }
    
    public function get_pages()
    {
        $query = DB::select()
                ->from('pages')
                ->order_by('sort', 'ASC');
            
        $pages = $query->execute()->as_array();

        return $pages;
    }
    
    public function get_max_sort()
    {
        $query = DB::select(array('MAX("sort")', 'max_sort'))
                    ->from('pages');
                    
        $result = $query->execute()->as_array();
                    
        return $result[0]['max_sort'];  
    }
    
    public function add_page($title, $url, $text, $sort, $description = null)
    {
        $query = DB::insert('pages', array('title', 'url', 'text', 'sort', 'description'))
                               ->values(array($title,  $url,  $text,  $sort,  $description));
                               
        $result = $query->execute();
        
        return $result[0];
    }
    
    public function update_page($title, $url, $text, $sort, $description, $page_id)
    {
        $page = array(
            'title' => $title,
            'url'   => $url,
            'text'  => $text,
            'sort'  => $sort,
            'description' => $description
        ); 
        
        $query = DB::update('pages')
                        ->set($page)
                        ->where('page_id', '=', $page_id);
                               
        $result = $query->execute();
        
        return $result;
    }
    
    public function delete_page($page_id)
    {
        $query = DB::delete('pages')
                    ->where('page_id', '=', $page_id);
                    
        $result = $query->execute();
        
        return $result;
    }
} // End Pagess